package com.aistar.serviceVO.impl;

import com.aistar.mapper.ChoiceCollectMapper;
import com.aistar.mapper.ChoiceMapper;
import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.Choice;
import com.aistar.pojo.ChoiceCollectExample;
import com.aistar.pojo.ChoiceCollect;
import com.aistar.pojo.Customer;
import com.aistar.pojoVO.ChoiceAndCollectVO;
import com.aistar.serviceVO.ChoiceAndCollectVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChoiceAndCollectVOServiceImpl implements ChoiceAndCollectVOService {
    private final ChoiceCollectMapper choiceCollectMapper;
    private final ChoiceMapper choiceMapper;
    private final CustomerMapper customerMapper;

    @Autowired
    public ChoiceAndCollectVOServiceImpl(ChoiceCollectMapper choiceCollectMapper,
                                         ChoiceMapper choiceMapper,
                                         CustomerMapper customerMapper) {
        this.choiceCollectMapper = choiceCollectMapper;
        this.choiceMapper = choiceMapper;
        this.customerMapper = customerMapper;
    }

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        ChoiceCollectExample example = new ChoiceCollectExample();
        example.createCriteria()
                .andCustomerIdEqualTo(customerId)
                .andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
        example.setOrderByClause("choice_collect_time desc");
        List<ChoiceCollect> choiceCollectList = choiceCollectMapper.selectByExample(example);
        if (choiceCollectList != null && choiceCollectList.size() > 0) {
            List<ChoiceAndCollectVO> choiceAndCollectVOList = new ArrayList<>();
            for (ChoiceCollect choiceCollect : choiceCollectList) {
                Choice choice = choiceMapper.selectByPrimaryKey(choiceCollect.getChoiceId());
                Customer customer = customerMapper.selectByPrimaryKey(choiceCollect.getCustomerId());
                choiceAndCollectVOList.add(new ChoiceAndCollectVO(choiceCollect, choice, customer));
            }
            return ServerResponse.getSuccess(choiceAndCollectVOList);
        }
        return ServerResponse.getFailed();
    }
}
